package com.gxa.saas.controller;

import com.gxa.saas.pojo.PriceSystem;
import com.gxa.saas.service.PriceSystemService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @CrossOrigin 跨域请求
 * @Author: yx
 * @Date: 2022/6/6 19:09
 */
@RestController
@RequestMapping("/priceSystem")
@CrossOrigin
public class PriceSystemController {

    @Autowired
    PriceSystemService priceSystemService;

    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation("查询全部")
    @PostMapping("/selectAll")
    public Map selectAll() {
        List<PriceSystem> priceSystems = priceSystemService.selectAll();
        Integer totalCount = priceSystemService.selectAllNumber();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.set("totalCount", totalCount);
        jsonResult.setData(priceSystems);
        return jsonResult.getMap();
    }

    /**
     * 分页查询的方法
     *
     * @param offset 查询第几页
     */
    @ApiOperation(value = "分页查询价格体系", produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "查询页数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页查询数量", paramType = "query", dataType = "int")
    })
    @PostMapping("/selectByPage")
    public Map selectByPage(@RequestParam("offset") Integer offset, @RequestParam("size") Integer size) {
        List<PriceSystem> priceSystems = priceSystemService.selectByPage(offset, size);
        Integer totalCount = priceSystemService.selectAllNumber();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(priceSystems);
        jsonResult.set("totalCount", totalCount);
        return jsonResult.getMap();
    }

    /**
     * 添加价格体系的方法
     *
     * @param priceSystem
     * @return
     */
    @ApiOperation(value = "添加价格体系")
    @ApiImplicitParam(name = "priceSystem", value = "要添加的价格体系", paramType = "body", dataType = "PriceSystem")
    @PostMapping("/insert")
    public Map insert(@RequestBody PriceSystem priceSystem) {
        priceSystemService.insert(priceSystem);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }

    /**
     * 根据id修改价格体系的方法
     *
     * @param priceSystem
     */
    @ApiOperation(value = "根据id修改价格体系")
    @ApiImplicitParam(name = "priceSystem", value = "修改好的价格体系", paramType = "body", dataType = "PriceSystem")
    @PostMapping("/update")
    public Map update(@RequestBody PriceSystem priceSystem) {
        priceSystemService.update(priceSystem);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    /**
     * 根据id删除价格体系的方法
     *
     * @param id
     */
    @ApiOperation(value = "根据id删除价格体系")
    @ApiImplicitParam(name = "id", value = "编号")
    @PostMapping("/delete")
    public Map delete(@RequestParam("id") Integer id) {
        priceSystemService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }


}
